package trees;

import java.util.*;

public class IsValid {
    public boolean isValid(String s) {
        if (s.equals("") || s.length() % 2 != 0) {
            return false;
        }

        Stack<String> stack = new Stack<>();
        int sLen = s.length();

        Map<String, String> maps = new HashMap<>();
        maps.put("(", ")");
        maps.put("[", "]");
        maps.put("{", "}");

        for (int i = 0; i < sLen; i++) {
            String currentVal = String.valueOf(s.charAt(i));
            if ("(".equals(currentVal) || "[".equals(currentVal) || "{".equals(currentVal)) {
                stack.push(currentVal);
            } else if (stack.isEmpty() || !currentVal.equals(maps.get(stack.peek()))) {
                return false;
            } else {
                stack.pop();
            }
        }


        return stack.isEmpty();
    }

    public static void main(String[] args) {
        String s = "){";
        IsValid isValid = new IsValid();
        System.out.println(isValid.isValid(s));
    }
}
